const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    API_BASE_URL: util.API_BASE_URL,
    inputValue: '',
    textareaValue: '',
    textareaNum: 0,
    pics: []
  },
  //提交
  submit() {
    const { inputValue, textareaValue, pics } = this.data;
    if (!inputValue) return util.showMsg("建议标题不能为空！");
    if (!textareaValue) return util.showMsg("建议内容不能为空！");

    wx.showLoading({
      title: '提交中...',
      mask: true
    })
    util.request('itf/common/hongpei/suggestion/saveSuggestion.json?type=admin', {
      bt: inputValue,
      nr_memo: textareaValue,
      tps: pics
    }, "POST").then(res => {
      wx.hideLoading();
      if (res.success) {
        util.showMsg({
          msg: "提交成功",
          icon: 'success',
          success: () => {
            wx.navigateBack();
          }
        })
      } else {
        util.showMsg({ msg: res.message || res.msg });
      }
    });
  },
  // 监听建议内容
  onTextareaChange(e) {
    const value = e.detail.value;
    this.setData({ textareaNum: value.length })
  },
  // 预览图片
  previewPic(e) {
    let { pics, API_BASE_URL } = this.data;
    let list = [];
    pics.forEach(item => {
      list.push(`${API_BASE_URL}itf/common/att/download.do?attrId=${item}`)
    })
    wx.previewImage({ urls: list })
  },
  // 删除图片
  handleDelete(e) {
    const { index } = e.currentTarget.dataset;
    let { pics } = this.data;
    pics.splice(index, 1);
    this.setData({ pics });
  },
  chooseImageTap: function (e) {
    let _this = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#F32F1D",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            _this.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            _this.chooseWxImage('camera')
          }
        }
      }
    })
  },
  chooseWxImage: function (type) {
    let _this = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      count: 6,
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        let info = wx.getAccountInfoSync()
        for (const key in tempFilePaths) {
          wx.uploadFile({
            url: util.API_BASE_URL + 'itf/common/att/edit.do',
            filePath: tempFilePaths[key],
            name: 'file',
            formData: {
              'dir': 'hongpei',
            },
            header: {
              'maAppId': info.miniProgram.appId || '',
              'content-type': 'multipart/form-data',
              'X-EOA-TOKEN': wx.getStorageSync('X-EOA-TOKEN')
            },
            success: function (res) {
              let picObj = JSON.parse(res.data);
              let { pics } = _this.data;
              pics.push(picObj.id);
              _this.setData({
                pics
              })
            },
            fail(err) {
              console.error(err);
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})